package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.service.*;
import org.example.mrrope.utils.DingUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private DingUtil dingUtil;
    @Autowired
    PackagingWorkOrder packagingWorkOrder;
    @Autowired
    RopeWorkOrder ropeWorkOrder;
    @Autowired
    FengRenWorkOrder fengRenWorkOrder;
    @Autowired
    CuttingRopeWorkOrder cuttingRopeWorkOrder;
    @Autowired
    DoKnotWorkOrder doKnotWorkOrder;


    @Override
    @Async
    public Integer getWorkOrder(String id, String isPackaging, String userId) {
        log.info("id " + id);
        Integer res = 1;
        try {
            //得到生产计划表数据
            String yidaJson = dingUtil.getYidaById(id);
            log.info("宜搭计划单数据" + yidaJson);
            JSONObject jsonObject = new JSONObject(yidaJson);
            log.info("getYidaById--得到数据" + jsonObject);
            JSONArray jsonArray = jsonObject.getJSONArray("checkboxField_lx2luc3u");
            log.info("得到数据" + jsonArray.toString());
            String type = "";
            String tasks = "";
            if (!jsonArray.isEmpty()) {
                JSONObject upJson = new JSONObject();
                for (int i = 0; i < jsonArray.length(); i++) {
                    type = jsonArray.getString(i);
                    if (isPackaging.equals("是")) {
                        tasks += this.processSelection(id, isPackaging, type, jsonObject, userId);
                        break;
                    } else {
                        tasks += this.processSelection(id, isPackaging, type, jsonObject, userId);
                    }
                }
                String yidaJsonNew = dingUtil.getYidaById(id);
                JSONObject jsonObjectNew = new JSONObject(yidaJsonNew);
                if (isPackaging != "是" && tasks != "") {
                    upJson.put("radioField_m1k25hzn", "已下发");
                    upJson.put("radioField_m1k25hzi", jsonObjectNew.getString("radioField_m1k25hzi"));
                }
                if (isPackaging == "是" && tasks != "") {
                    upJson.put("radioField_m1k25hzi", "已下发");
                    upJson.put("radioField_m1k25hzn", jsonObjectNew.getString("radioField_m1k25hzn"));
                }
                //得到生产计划表数据
                upJson.put("radioField_m2bc8txg", jsonObjectNew.getString("radioField_m2bc8txg"));
                upJson.put("radioField_m2woaxgr", jsonObjectNew.getString("radioField_m2woaxgr"));
                upJson.put("radioField_m2bc8txh", jsonObjectNew.getString("radioField_m2bc8txh"));
                String upres = dingUtil.UpdateYiDa(id, upJson);
                log.info("UpdateYiDa--派工完成更新结果" + upres);
            } else {
                // 处理键不存在或不是数组的情况
                System.out.println("键 checkboxField_lx2luc3u 不存在或不是数组类型。");
            }
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 选择下发的工序
     *
     * @param id
     * @param isPackaging
     * @param type
     * @param jsonObject
     */
    public String processSelection(String id, String isPackaging, String type, JSONObject jsonObject, String userId) throws Exception {
        Integer res = 1;
        String tasks = "";
        if (isPackaging.equals("是")) {
            //包装车间派工单
            res = packagingWorkOrder.savePackagingWorkOrder(jsonObject, id, userId);
            if (res == 0) {
                tasks = tasks + ";包装车间";
                String upresult = dingUtil.UpdateYiDa(id, new JSONObject().put("textField_lywpgrkq", "已完成派工" + tasks));
                log.info("更新包装车间结果" + upresult);
            }
        } else if (type.equals("制绳") && isPackaging.equals("否")) {
            //绳子车间派工单
            res = ropeWorkOrder.saveRopeWorkOrder(jsonObject, id, userId);
            if (res == 0) {
                tasks = tasks + ";制绳车间";
            }
        } else if (type.equals("机器切绳") && isPackaging.equals("否")) {
            //切绳车间派工单
            res = cuttingRopeWorkOrder.saveCuttingRopeWorkOrder(jsonObject, id, userId);
            if (res == 0) {
                tasks = tasks + ";切绳车间";
            }
        } else if (type.equals("制结&手工") && isPackaging.equals("否")) {
            //制结&手工车间派工单
            res = doKnotWorkOrder.getDoKnot(jsonObject, id, userId);
            if (res == 0) {
                tasks = tasks + ";制结&手工车间";
            }
        } else if (type.equals("缝纫") && isPackaging.equals("否")) {
            res = fengRenWorkOrder.getFengRen(jsonObject, id, userId);
            if (res == 0) {
                tasks = tasks + ";缝纫车间";
            }
        } else {
            log.info("没有找到对应的车间");
        }
        return tasks;
    }

    @Override
    @Async
    public void createWorkGonxu(String id, String work, String userId) {
        //得到bom表数据
        String yidaJson = null;
        try {
            yidaJson = dingUtil.getYidaById(id);
            log.info("宜搭bom数据" + yidaJson);
            JSONObject jsonObject = new JSONObject(yidaJson);
            String isPackaging = "否";
            this.processSelection(id, isPackaging, work, jsonObject, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Integer test01(String id) {
        Integer res = null;
        //得到bom表数据
        JSONObject jsonObject = null;
        try {
            JSONObject object = new JSONObject();
            object.put("textField_lwbmqipf", "测试");
            String yidaById = dingUtil.UpdateYiDa(id, object);
            log.info("测试bom " + yidaById);
            if (yidaById.equals("")) {
                res = 1;
            }
            jsonObject = new JSONObject(yidaById);
            //绳子车间派工单
//            ropeWorkOrder.saveRopeWorkOrder(jsonObject,id);
            //切绳车间派工单
//            cuttingRopeWorkOrder.saveCuttingRopeWorkOrder(jsonObject, id);
            //制结手工
//            doKnotWorkOrder.getDoKnot(jsonObject, id);
            //包装
            //packagingWorkOrder.savePackagingWorkOrder(jsonObject, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }


}
